﻿using System.Data;
using Litografia.BusinessEntities;
using Nortec.Security;

namespace Litografia.DataObjects
{
    public sealed class DOGasto : DOBase
    {
        #region Declarations

        private const string getSP = "gastoGet";
        private const string updateSP = "gastoUpdate";
        private const string deleteSP = "gastoDelete";
        private const string addSP = "gastoAdd";

        #endregion

        #region Methods: Public

        public static BEGasto.GastoDataTable Get()
        {
            var result = new BEGasto();

            var command = DB.GetStoredProcCommand(getSP);
            DB.AddInParameter(command, "@EmpresaId", DbType.Int32, NortecMembership.CompanyId);
            var dataset = DB.ExecuteDataSet(command);

            // Pasa informacion hacia el BE
            if (dataset != null && dataset.Tables.Count > 0)
            {
                dataset.Tables[0].TableName = result.Tables[0].TableName;
                result.Merge(dataset);
                result.EnforceConstraints = true;
            }
            return result.Gasto;
        }

        public static void Update(BEGasto.GastoRow gasto)
        {
            var command = DB.GetStoredProcCommand(updateSP);
            DB.AddInParameter(command, "@id", DbType.Int32, gasto.Id);
            DB.AddInParameter(command, "@concepto", DbType.String, gasto.Concepto);
            DB.AddInParameter(command, "@monto", DbType.Decimal, gasto.Monto);
            DB.AddInParameter(command, "@tipo", DbType.String, gasto.Tipo);
            DB.ExecuteNonQuery(command);
        }

        public static void Delete(int id)
        {
            var command = DB.GetStoredProcCommand(deleteSP);
            DB.AddInParameter(command, "@id", DbType.Int32, id);
            DB.ExecuteNonQuery(command);
        }
        
        public static void Add(BEGasto.GastoRow gasto)
        {
            var command = DB.GetStoredProcCommand(addSP);
            DB.AddInParameter(command, "@empresaId", DbType.Int32, gasto.EmpresaId);
            DB.AddInParameter(command, "@concepto", DbType.String, gasto.Concepto);
            DB.AddInParameter(command, "@monto", DbType.Decimal, gasto.Monto);
            DB.AddInParameter(command, "@tipo", DbType.String, gasto.Tipo);
            DB.ExecuteNonQuery(command);
        }

        #endregion
    }
}
